package com.sha.mumbailocal.server.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.sha.mumbailocal.server.exception.ContentUnavailableException;
import com.sha.mumbailocal.server.intf.IContentProvider;

public class UrlContentProvider implements IContentProvider {

	private String sourceUrl;

	public UrlContentProvider(String url) {
		sourceUrl = url;
	}
	
	@Override
	public Reader getContentReader() throws ContentUnavailableException {
		BufferedReader reader = null;
		try {
			URL url = new URL(sourceUrl);
			URLConnection urlConn = url.openConnection();
			urlConn.setRequestProperty("Referer","http://erail.in/MumbaiSubUrbanTrains.htm");
			urlConn.connect();
			reader = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
		} catch (MalformedURLException e) {
			throw new ContentUnavailableException(e.getMessage(),e);
		} catch (IOException e) {
			throw new ContentUnavailableException(e.getMessage(),e);
		}
		return reader;
	}

	public String getSourceUrl() {
		return sourceUrl;
	}

	public void setSourceUrl(String sourceUrl) {
		this.sourceUrl = sourceUrl;
	}

}
